import 'dart:convert';

import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:dali_ng_pera/net/dnp_net_manager.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../mob/dnp_mob.dart';
import 'model/dnp_home_data_model.dart';

class DnpHomeTabController extends GetxController {
  final refreshController = RefreshController();
  final status = RxStatus.loading().obs;
  final homeDataModel = DnpHomeDataModel().obs;

  final largeCard = true.obs;

  @override
  void onReady() {
    super.onReady();
    ever(homeDataModel, (data) {
      List<DnpSyllable> syllables = homeDataModel().syllables ?? [];
      for (var syllable in syllables) {
        if (syllable.heard == "ob") {
          largeCard.value = true;
        } else if (syllable.heard == "oc") {
          largeCard.value = false;
        }
      }
    });
  }

  void onRequestData({bool showLoading = false}) {
    status.value = RxStatus.loading();
    if(showLoading){
      DnpPluginManager().showLoading();
    }
    DnpNetManager().get("/haseye/reverent").then((value) {
      if(showLoading){
        DnpPluginManager().hideLoading();
      }
      refreshController.refreshCompleted();
      if (value.iesous != "0") {
        status.value = RxStatus.error(value.kurios);
      } else {
        homeDataModel.value =
            DnpHomeDataModel.fromJson(value.close);
        status.value = RxStatus.success();

        DnpMob.indescribable();
        DnpMob.fervour();
        DnpMob.within();
      }
    });
  }
}
